home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_locale.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  1.4 KB  |  43 lines

  1. from test_support import verbose
  2. import locale
  3. import sys
  4.  
  5. oldlocale = locale.setlocale(locale.LC_NUMERIC)
  6.  
  7. tloc = "en_US"
  8. if sys.platform[:3] == "win":
  9.     tloc = "en"
  10.  
  11. try:
  12.     locale.setlocale(locale.LC_NUMERIC, tloc)
  13. except locale.Error:
  14.     raise ImportError, "test locale %s not supported" % tloc
  15.  
  16. def testformat(formatstr, value, grouping = 0, output=None):
  17.     if verbose:
  18.         if output:
  19.             print "%s %% %s =? %s ..." %\
  20.                 (repr(formatstr), repr(value), repr(output)),
  21.         else:
  22.             print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
  23.     result = locale.format(formatstr, value, grouping = grouping)
  24.     if output and result != output:
  25.         if verbose:
  26.             print 'no'
  27.         print "%s %% %s == %s != %s" %\
  28.               (repr(formatstr), repr(value), repr(result), repr(output))
  29.     else:
  30.         if verbose:
  31.             print "yes"
  32.  
  33. try:
  34.     testformat("%f", 1024, grouping=1, output='1,024.000000')
  35.     testformat("%f", 102, grouping=1, output='102.000000')
  36.     testformat("%f", -42, grouping=1, output='-42.000000')
  37.     testformat("%+f", -42, grouping=1, output='-42.000000')
  38.     testformat("%20.f", -42, grouping=1, output='                 -42')
  39.     testformat("%+10.f", -4200, grouping=1, output='    -4,200')
  40.     testformat("%-10.f", 4200, grouping=1, output='4,200     ')
  41. finally:
  42.     locale.setlocale(locale.LC_NUMERIC, oldlocale)
  43.